import axios from 'axios'
import { THREE } from 'threebox-plugin'
import Constants from './constants'
import legendInfo from './legend_info'
import util from './util'

const basicMaterial = new THREE.MeshBasicMaterial({
  color: '#CCCCCC',
  opacity: 0,
  transparent: true
})

const selectedMaterial = new THREE.MeshBasicMaterial({
  color: '#4BF5F1',
  opacity: 0.5,
  transparent: true
})

class RightManager {
  constructor(vueInstance) {
    this.map = vueInstance.map
    this.tb = vueInstance.threeBoxLayer.tb
    this.threeBoxLayer = vueInstance.threeBoxLayer
    this.vueInstance = vueInstance
    this.rightObjects = []
    this.loadedProperties = []  // 记录已经加载属性数据的建筑id
    this.init()
  }

  init () {
    // 添加外部楼层模型数据
    axios.get('./models/rights.json').then(res => {
      const outerBuildingList = res.data
      outerBuildingList.forEach(building => {
        let options = {
          type: 'gltf',
          obj: `./models/江湾雅园/outer/${building.model}`,
          units: 'meters',
          rotation: { x: 0, y: 0, z: 180 },
          anchor: 'auto',
          modelType: 'right',
          name: building.name,
          bbox: false,
          ...building.properties
        }
        this.tb.loadObj(options, (model) => {
          model.setCoords(Constants.origin)
          model.name = name
          const meshList = model.model.children
          meshList.forEach(mesh => {
            mesh.material = basicMaterial
          })
          this.tb.add(model)
          this.rightObjects.push(model)
        })
      })
    })
  }

  updateStatus() {
    this.rightObjects.forEach(rightBuilding => {
      this.updateBuildingStatus(rightBuilding)
    })
  }

  updateBuildingStatus(building) {
    let { buildingId } = building.userData
    if (this.loadedProperties.indexOf(buildingId) !== -1) {
      this._updateBuilding(building)
    } else {
      axios.get(`./properties/${buildingId}.json`).then(res => {
        let buildingInfo = res.data
        this._updateBuilding(building, buildingInfo)
        this.loadedProperties.push(buildingId)
      })
    }
  }

  _updateBuilding(building, buildingInfo) {
    let rightStatus = this.vueInstance.$store.state.showBuildingsRight
    let opacity = this.vueInstance.$store.state.buildingRightOpacity
    let selectedHouse = this.vueInstance.$store.state.houseInfo
    let mode = this.vueInstance.$store.state.mode
    let { buildingId } = this.vueInstance.$store.state.buildingOverview
    building.traverse((mesh) => {
      if (mesh.isMesh) {
        if (selectedHouse && selectedHouse['模型名称'] === mesh.name) { // 选中的mesh不做处理
          return
        }
        if (!rightStatus || (mode !== 'global' && buildingId !== building.userData.buildingId)) {
          mesh.material = basicMaterial
          return
        }
        let houseInfo = {}
        if (buildingInfo) {
          houseInfo = buildingInfo.find(item => item['模型名称'] == mesh.name) || mesh.userData
          Object.assign(mesh.userData, houseInfo)
        } else {
          houseInfo = mesh.userData
        }
        let color = legendInfo[rightStatus][houseInfo[rightStatus]] || Constants.defaultColor
        mesh.material = new THREE.MeshBasicMaterial({
          color,
          opacity,
          transparent: true
        })
      }
    })
  }

  easeOut(floor) {
    if (this.isAnimate) {
      return
    }
    let { buildingId, bearing } = this.vueInstance.$store.state.buildingOverview
    let buildingObject = this.rightObjects.find(item => item.userData.buildingId == buildingId)
    buildingObject.traverse(mesh => {
      if (mesh.isMesh) {
        let meshFloor = Number(mesh.name.slice(3, 5))
        if (meshFloor !== floor) {
          return
        }
        this.threeBoxLayer.floorPopAnimate(mesh, Constants.animateVec, -bearing)
      }
    })
    //this.threeBoxLayer.adjustFreeCamera(floorObject)
  }

  easeIn(floor) {
    if (this.isAnimate) {
      return
    }
    let { buildingId, bearing } = this.vueInstance.$store.state.buildingOverview
    let buildingObject = this.rightObjects.find(item => item.userData.buildingId == buildingId)
    buildingObject.traverse(mesh => {
      if (mesh.isMesh) {
        let meshFloor = Number(mesh.name.slice(3, 5))
        if (meshFloor !== floor) {
          return
        }
        this.threeBoxLayer.floorPopAnimate(mesh, -Constants.animateVec, -bearing)
      }
    })
  }

  reset() {
    
  }

  hilightHouse() {
    let { buildingId } = this.vueInstance.$store.state.buildingOverview
    let houseInfo = this.vueInstance.$store.state.houseInfo
    let buildingObject = this.rightObjects.find(item => item.userData.buildingId == buildingId)
    this.updateBuildingStatus(buildingObject)
    buildingObject.traverse(mesh => {
      if (mesh.isMesh && houseInfo['模型名称'] && mesh.name === houseInfo['模型名称']) {
        mesh.material = selectedMaterial
      }
    })
  }

  flatternOthers() {
    console.log(this.rightObjects)
    let {buildingId} = this.vueInstance.$store.state.buildingOverview
    this.rightObjects.forEach( item => {
      if (item.userData.buildingId === buildingId) {
        return
      }
      util.flattern(item);
      item.removeTooltip();
      if (item.boundingBox) {
        item.boundingBox.visible = false;
      }
    })
  }

  extrustOthers() {
    let {buildingId} = this.vueInstance.$store.state.buildingOverview
    this.rightObjects.forEach( item => {
      if (item.userData.buildingId === buildingId) {
        return
      }
      util.extrust(item);
    })
  }
}

export default RightManager